/**
 * Created by tongjunshui on 17/6/9.
 */
var express = require('express');
var router = express.Router();

var settings = require('../../settings');
var log = require('../../logger.js');
var md5 = require('md5');

var superagent = require('superagent');

var dbDao = require('../../models/dbBase.js');


router.get('/', function (req,res){
    var orderNo = req.params.orderNo || '';
    var wxopenid = req.query.wxopenid || '';

    console.log("这个页面点击后微信收款",orderNo,'wxid:',wxopenid);
    if((!orderNo)&&(wxopenid)){
        res.send({result:'error',message:'订单号或微信openid无效...'});
    }else{
        var sql = "select * from orders where id='" + orderNo + "'";
        dbDao.find( sql, function (err, result) {
            if (result.length > 0) {
                var sql2 = "select * from Gymnasium where id='" + result[0].gymId + "'";
                dbDao.find(sql2, function (err, result2) {
                    //})opo4Fws5LKKN9UFG2jvfz4N-jz1k
                    superagent.get('http://go.sportscv.cn:8804/webpay')
                        .query('openid='+wxopenid)
                        .query('out_trade_no='+orderNo)
                        .end(function(err, result){
                            if(err){
                                console.log("error:", err);
                                res.send({result:'error',message:''});
                            }else{
                                if(result&&result.body){
                                    console.log(result.body);
                                    res.send({result:'success',data:result.body});
                                }else{
                                    res.send({result:'error',message:''})
                                }
                            }
                        });
                });
            } else {
                log.logger.info('查不到订单err：' + err + 'orderNo=' + orderNo);
                res.send({result:'error',message:'订单号无效...'});
            }
        });
    }
});

router.get('/getWxSignature', function (req,res){

    var qUrl = req.headers.referer;
    console.log("---params:", qUrl);
    superagent.get(settings.gymPayApiBaseUrl+'/getSignature')
        .query('qurl='+qUrl)
        .end(function(err, result){
            if(err){
                console.log("error:", err);
                res.send({result:'error',message:''});
            }else{
                if(result&&result.body){
                    console.log(result.body);
                    res.send({result:'success',data:result.body});
                }else{
                    res.send({result:'error',message:'获取失败'})
                }
            }
        });

})
module.exports = router;